<?php

function delete_attachments($post_id) {
    global $aldb;
    $files = $aldb->get_results("SELECT `upload_time`,`extra` $aldb->attachments WHERE post_id='$post_id'");
    if (!empty($files)) {
        $this->Attachments->del_all("post_id='$id'");
            foreach($files as $file) {
                $file_url = ROOT . 'www/attach/' . date('Y', $file->upload_time) . '/' . date('n', $file->upload_time) . '/' . date('j', $file->upload_time) . '/' . $file->upload_time . '.' . $file->extra;
                if (file_exists($file_url)) {
                    unlink($file_url);
                }
            }
    }
    return true;
}

function get_attach_list($post_id) {
    global $aldb;
    $attachments = $aldb->get_results("SELECT * FROM $aldb->attachments WHERE post_id='$post_id'");
    $attach['list'] = null;
    $attach['size'] = 0;
    if (!empty($attachments)) {
        foreach($attachments as $attachment) {
            $attach['list'] .= '<option value="' . $attachment->upload_time . '|' . $attachment->extra . '|' . $attachment->width . '|' . $attachment->height . '|' . $attachment->size . '">' . $attachment->name . ' (';
            if (!empty($attachment->width)) {
                $attach['list'] .= $attachment->width . 'x' . $attachment->height . ' / ' . al_get_size($attachment->size);
            } else {
                $attach['list'] .= al_get_size($attachment->size);
            }
            $attach['list'] .= ')' . '</option>';
//            $attach['size'] += $attachment->size;
        }
    }
//    $attach['size'] = al_get_size($attach['size']);
    return $attach;
}
?>
